<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="元素" prop="qualityCate">
        <el-input v-model="dataForm.qualityCate" placeholder="元素"></el-input>
      </el-form-item>
      <el-form-item label="权重" prop="scoreRadio">
        <el-input v-model="dataForm.scoreRadio" placeholder="权重" ></el-input>
      </el-form-item>
      <el-form-item label="类型 " prop="type">
        <el-input v-model="dataForm.type" placeholder="类型(A、B、C、D)"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" type="textarea" :rows="5" placeholder="描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          qualityCate: '',
          scoreRadio: '',
          type: '',
          description: ''
        },
        dataRule: {
          qualityCate: [
            { required: true, message: '元素不能为空', trigger: 'blur' }
          ],
          scoreRadio: [
            { required: true, message: '权重不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型(A、B、C、D)不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/project/qulaitymark/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.qualityCate = data.qulaityMark.qualityCate
                this.dataForm.scoreRadio = data.qulaityMark.scoreRadio
                this.dataForm.type = data.qulaityMark.type
                this.dataForm.description = data.qulaityMark.description
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/project/qulaitymark/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'qualityCate': this.dataForm.qualityCate,
                'scoreRadio': this.dataForm.scoreRadio,
                'type': this.dataForm.type,
                'description': this.dataForm.description
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
